home *** CD-ROM | disk | FTP | other *** search
- // PlayTool
- //
- // This is an MPW tool which plays sounds. Given a list of sound resource names, it will play one
- // sounds (for example, if they're in the System file). It will pick the sound to play at random. If
- // a "-a" option is given, it will play all of the specified sounds in turn. If no sounds are specified,
- // it will look in itself and use any sounds found there as the sound list to pick from at random, or
- // to play all of (-a). It is, therefore, possible to make a copy of PlayTool by a different name,
- // and then paste a set of sounds into it. When that new tool is invoked, it will play one of the sounds
- // that were pasted into it. Thus, it can be used to make "build completed" and "build failed" tools,
- // amoung others.
- //
- // Things that could be added to make it even better:
- // * Specifying a file to read the sound(s) from.
- // * Volume control.
- //
- // Initial coding 7/91 by Harry Chesley.
- //
- // © Copyright 1991 by Apple Computer, Inc.
- // All rights reserved.
-
- #include <Types.h>
- #include <ctype.h>
- #include <string.h>
- #include <strings.h>
- #include <stdio.h>
- #include <Events.h>
- #include <Sound.h>
- #include <Resources.h>
-
- #define NIL 0
-
- main(int argc, char *argv[])
- {
- int allPlayOption;
- int localOption;
- int i;
- int names;
- int length;
- Handle h;
- Str255 theName;
-
- names = 0;
- allPlayOption = false;
- localOption = false;
-
- // Parse the parameter list:
- for (i = 1; i < argc; i++) {
- length = strlen(argv[i]);
- if (*argv[i] != '-') argv[++names] = argv[i];
- else if (tolower(*(argv[i]+1)) == 'a' && length == 2) allPlayOption = true;
- else {
- fprintf(stderr,"### %s - \"%s\" is not an option.\n", argv[0], argv[i]);
- fprintf(stderr,"# Usage - %s [-a] [sound…].\n", argv[0]);
- return 1;
- }
- }
-
- // If we weren't given any sound names, try introspection:
- if (names == 0) {
- names = Count1Resources('snd ');
- localOption = true;
- }
-
- // If there's still nothing to play, tell the user how to call us:
- if (names == 0) {
- fprintf(stderr, "# Usage - %s [-a] [sound…].\n", argv[0]);
- return 1;
-
- // Otherwise, play, play, play:
- } else {
- // Decide between playing only one or everything in turn:
- if (allPlayOption) i = 1;
- else {
- i = TickCount() % names + 1;
- names = i;
- };
- // Cycle through all the sounds to be played:
- for (; i <= names; i++) {
- // Get the sound (either locally or in any file in the resource chain (probably the System file)):
- if (localOption) h = Get1IndResource('snd ',i);
- else {
- if (strlen(argv[i]) > 255) {
- fprintf(stderr,"Sound name too long (> 255)!\n");
- continue;
- };
- strcpy(&theName,argv[i]);
- c2pstr(theName);
- h = GetNamedResource('snd ',theName);
- }
- // Play it:
- if (ResError() == 0) SndPlay(NIL,h,false);
- else fprintf(stderr,"Could not find sound!\n");
- }
- }
-
- // Return success:
- return 0;
- }
-